package com.cqjtu.store.order.controller;

import com.cqjtu.store.common.JwtUtils;
import com.cqjtu.store.common.PageDTO;
import com.cqjtu.store.common.PageVO;
import com.cqjtu.store.common.R;
import com.cqjtu.store.order.pojo.DTO.OrderPageDTO;
import com.cqjtu.store.order.pojo.DTO.SetOrderDTO;
import com.cqjtu.store.order.pojo.DTO.TimeDTO;
import com.cqjtu.store.order.pojo.vo.OrderListVO;
import com.cqjtu.store.order.pojo.vo.OrderTotalVO;
import com.cqjtu.store.order.pojo.vo.OrderVO;
import com.cqjtu.store.order.pojo.vo.SetOrderVO;
import com.cqjtu.store.order.service.OrderService;
import com.cqjtu.store.user.pojo.vo.UserLoginVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.List;

@RestController
@RequestMapping("api/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @GetMapping("list")
    public R<List<OrderVO>> getOrderList(HttpServletRequest request){
        String token = request.getHeader("Authorization");
        UserLoginVO userLoginVO = JwtUtils.parseToken(token);
        List<OrderVO> orderVOList = orderService.getList(userLoginVO);
        return R.ok(orderVOList);
    }
    @GetMapping("status/{status}")
    public R<List<OrderVO>> getOrderListByStatus(@PathVariable Integer status, HttpServletRequest request){
        String token = request.getHeader("Authorization");
        UserLoginVO userLoginVO = JwtUtils.parseToken(token);
        List<OrderVO> orderVOList =  orderService.getListByStatus(status,userLoginVO);
        return R.ok(orderVOList);
    }
    //生成订单
    @GetMapping("/setOrder")
    public R<SetOrderVO> setOrder(@RequestBody SetOrderDTO setOrderDTO, HttpServletRequest request){
        String token = request.getHeader("Authorization");
        UserLoginVO userLoginVO = JwtUtils.parseToken(token);
        SetOrderVO setOrderVO=orderService.insertOrder(setOrderDTO,userLoginVO);
        return R.ok(setOrderVO);

    }

    @GetMapping("getRealTimeNum")
    public R<Integer> getRealTimeNum(){
        int reslut = orderService.getRealTimeNum();
        return R.ok(reslut);
    }

    @PostMapping("getTotalInTime")
    public R<List<OrderTotalVO>> getTotalInTime(@RequestBody TimeDTO timeDTO){
        List<OrderTotalVO> orderTotalVOList = orderService.getTotalInTime(timeDTO);
        return R.ok(orderTotalVOList);
    }

    @GetMapping("getUserAndProductOrder/{userId}/{productId}")
    public R<List<OrderTotalVO>> getUserAndProductOrder(@PathVariable Integer userId,@PathVariable Integer productId){
        List<OrderTotalVO> orderTotalVOList = orderService.getUserAndProductOrder(userId,productId);
        return R.ok(orderTotalVOList);
    }
    @GetMapping("getProductOrder/{productId}")
    public R<List<OrderTotalVO>> getProductOrder(@PathVariable Integer productId){
        List<OrderTotalVO> orderTotalVOList = orderService.getProductOrder(productId);
        return R.ok(orderTotalVOList);
    }
    @GetMapping("getUserOrder/{userId}")
    public R<List<OrderTotalVO>> getUserOrder(@PathVariable Integer userId){
        List<OrderTotalVO> orderTotalVOList = orderService.getUserOrder(userId);
        return R.ok(orderTotalVOList);
    }
    @PostMapping("getAllOrder")
    public R<PageVO<List<OrderListVO>>> getAllOrder(@RequestBody @Validated PageDTO pageDTO){
        PageVO<List<OrderListVO>> orderListVOList = orderService.getAllOrder(pageDTO);
        return R.ok(orderListVOList);
    }

    @PostMapping("getOrderByKey")
    public R<PageVO<List<OrderListVO>>> getOrderByKey(@RequestBody @Validated OrderPageDTO pageDTO){
        PageVO<List<OrderListVO>> orderListVOList = orderService.getOrderByKey(pageDTO);
        return R.ok(orderListVOList);
    }

    @DeleteMapping("deleteOrder/{id}")
    public R<Void> deleteOrder(@PathVariable Integer id){
        orderService.deleteOrder(id);
        return R.ok();
    }

    @GetMapping("getAllOrderNum")
    public R<Integer> getAllOrderNum(){
        int num = orderService.getAllOrderNum();
        return R.ok(num);
    }
}
